package br.edu.ifrn.sgp.resources;

import java.util.ArrayList;
import java.util.Collection;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import br.edu.ifrn.sgp.arq.util.ValidatorUtil;
import br.edu.ifrn.sgp.dtos.DocumentoDTO;
import br.edu.ifrn.sgp.protocolo.dao.DocumentoDAO;
import br.edu.ifrn.sgp.protocolo.dominio.Documento;

@Path("/documento")
public class DocumentoResource {
	
	@GET
	@Path("/documentosUsuario")
    @Produces("application/json")
    public Response getDocumentosUsuario(@QueryParam("idUsuario") String idUsuario){
		
		Response response;
		DocumentoDAO dao = new DocumentoDAO();
		Integer id = Integer.parseInt(idUsuario);
		
		Collection<Documento> documentos = dao.findByExactField("usuario.id", id, Documento.class);
		
		if (ValidatorUtil.isEmpty(documentos)){
			response = Response.status(Response.Status.OK).entity(null).build();
			return response;
		}
		
		Collection<DocumentoDTO> result = new ArrayList<DocumentoDTO>();
		
		for (Documento doc : documentos){
			result.add(DTOHelper.documentoToDTO(doc));
		}
		
    	return response = Response.status(Response.Status.OK).entity(result).build();
    }
}
